package Servlets.lhl;/**
 * 项目名称：Purchase_orderServlet.java
 * 文件名称：Purchase_orderServlet.java
 * 类的描述：无
 * 创建时间：2025-07-25 13:26:37
 * 创建人：大帅比
 * <p>
 * 版权声明：本文件归 大帅比 所有，未经授权不得进行商业使用。
 */
 /*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/

import java.io.*;
import java.util.List;

import Bean.purchase_order;
import Util.GokJDBCUtil2;
import Util.RES;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@WebServlet(name = "Purchase_orderServlet", value = "/purchase_order")
public class Purchase_orderServlet extends HttpServlet {
    public void init() {
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
       //获取page参数
        String page = req.getParameter("page");
        //判断page是否为空或这是否为空字符串
        if (page != null && !"".equals(page)) {
            //将page转换为int类型
            Integer p = Integer.parseInt(page);
            Integer pageSize = 10;
            String sql="select id,code,supplierid,(select supplier.name from supplier where purchase_order.supplierid=supplier.id  ) as suname ,ordertime,totalamount,orderstatus,remark from purchase_order where isdeleted=0 order by id desc limit ?,?";
            List<purchase_order> purchaseOrders = GokJDBCUtil2.queryAll(sql, purchase_order.class, (p - 1) * pageSize, pageSize);
            req.setAttribute("list", purchaseOrders);
            //设置总页数
            int i = GokJDBCUtil2.queryForCount("select count(*) from purchase_order where isdeleted = 0");
            req.setAttribute("count", i%pageSize==0?i/pageSize:i/pageSize+1);
            req.getRequestDispatcher("pages/purchase.jsp").forward(req,res);
        }else{
            //返回错误信息
            res.getWriter().write(RES.fail("page不能为空"));
        }
    }
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String method = req.getMethod();
        try {
            if (method.equals("GET")) {
                this.doGet(req, res);
            }else {
                PrintWriter writer = res.getWriter();
                writer.println("{\"code\":\"400\",\"msg\":\"此方法不允许\"}");
                writer.close();
            }
        } catch (Exception e) {
            log.error("服务器处理请求时发生错误", e);
            PrintWriter writer = res.getWriter();
            writer.println("{\"code\":\"500\",\"msg\":\"服务器处理请求时发生错误\"}");
            writer.close();
        }
    }

    public void destroy() {
    }
}